import { Component, OnInit, Input, ViewChild, ElementRef, AfterViewInit } from '@angular/core';
import { Topology } from 'topology-core';
import { register as registerFlow } from 'topology-flow-diagram';
import {register as registerCustom} from '../shape/register';
import { NzMessageService } from 'ng-zorro-antd';
import { HttpService } from 'lbf';
@Component({
  selector: 'app-flow-view',
  templateUrl: './flow-view.component.html',
  styleUrls: ['./flow-view.component.less']
})
export class FlowViewComponent implements OnInit,AfterViewInit {

  @Input()
  delay = 0;
  @Input()
  id :number
  @Input()
  name = ''

  start = {
    'id': 'start',
    'name': 'rectangle',
    'tags': [],
    'text': '开始',
    'rect': {
      'x': 50,
      'y': 216,
      'width': 50,
      'height': 50,
      'center': { 'x': 404, 'y': 266 },
      'ex': 454,
      'ey': 316
    },
  }

  data;
  canvas: Topology;
  canvasOptions = {
    on: (event: string, data: any) => { }
  }

  @ViewChild('view', { static: true }) view: ElementRef;
  constructor(
    public msgSrv: NzMessageService,
    private lbserver: HttpService,
  ) {
    registerFlow();
    registerCustom();
   }

  ngOnInit() {
  }


  ngAfterViewInit() {

    this.lbserver.lbservice('FLOW_QUERY', { para: { DAD017: this.id } }).then(res => {

      if (res) {
        if(res.para){
            // const para = JSON.parse(res.message.list[0].DAD038)
             this.data = res.para.data
        }else{
          this.init()
        }
      
      } else {
        this.init()
        // this.msgSrv.error(res.errmsg)
      }
      setTimeout(() => {
        this.canvas = new Topology(this.view.nativeElement, this.canvasOptions);
        this.canvas.open(this.data);
        // this.canvas.data.pens = this.data.pens
        // this.topologySrv.canvasRegister();
        this.canvas.render();
        this.canvas.resize()
        this.canvas.lock(1)
      }, this.delay
      )
     
    })


  }

  init() {
    this.start.text = this.name
    this.data = {
      nodes: [
        this.start,

      ], lines: []
    }

}
}
